<?php
include_once('../../inc/db.php');
$attach_id = '';
$filenameUtf = '';
$attach_size = '';

function attach_name_regular($myvalue)
{
	$mainStr = preg_replace('/\s+|\+|-|\*|\/|_|\-|\`|\?|\"|\,|\'|@|#|\$|\!|\&|%|\^|;|\(|\)|=|\[|\]|{|}|\||\\\/','',$myvalue);
	$count =  substr_count($mainStr,'.');
	if($count == 0){
		return $mainStr.'.errtype';
	}else if($count == 1){
		return $mainStr;
	}else{
		return preg_replace('/\./','',$mainStr,$count-1);
		
	}
}

$userName = $_REQUEST['userName'];
$userName = urldecode($userName);
$toName = $_GET['toName'];
$toName = urldecode($toName);
$title = $_REQUEST['title'];
$title = urldecode($title);
$content = $_REQUEST['content'];
$content = urldecode($content);
$userId = $_REQUEST['userId'];
$sendertime = date("Y-m-d H:i:s");

$icontype = array("xls","xlsx","doc","docx","ppt","pptx","pps","ppsx","7z","rar","zip","iso","htm","html","js","css","exe","bat","ttf","pdf","psd","ai","swf","txt","rm","rmvb","wmv","avi","mp4","mpge","mp3","raw","cdr");	
if ($_FILES){//如果上传文件
    //print_r($_FILES);	
	$toName = $_POST['toName'];
	$toName = urldecode($toName);
	$toName = implode(',',json_decode($toName)).",";
	
	$filename = $_FILES["file"]["name"];
	$attach_size = $_FILES["file"]["size"];
	$filenameUtf = attach_name_regular(urldecode($filename));
	$filename = iconv("UTF-8","gb2312",$filenameUtf);
	$datedir = date('ym');
	$newemailrand = rand(1000000,9999999);
	$attach_id = $datedir.".".$newemailrand.",";
	$dirname = "../../attachment/email/".$datedir;
	if(!is_dir($dirname)) mkdir($dirname, 0755); 
	$suffix = end(explode(".",$filename));//按空格截取
	if (!in_array($suffix,$icontype)) {
		$filename = $filename.'.jpg';
		$filenameUtf = $filenameUtf.'.jpg,';
	}else{
		$filenameUtf = $filenameUtf.',';	
	}
    move_uploaded_file($_FILES["file"]["tmp_name"],"../../attachment/email/".$datedir."/".$newemailrand.".".$filename);
}
//if($toName != '' and $title != ''){	
	$sql = "INSERT INTO `zy_email_body` (`bodyid`, `sender`, `addressee2`, `title`, `content`, `sendertime`, `attachmentid`, `attachmentname`, `smsalerts`, `important`, `size`) VALUES (NULL, '".$userName."', '".$toName."', '".$title."', '".$content."', '".$sendertime."', '".$attach_id."', '".$filenameUtf."', '', '0', '".$attach_size."')";
	mysql_query($sql,$db);
	$insert_id = mysql_insert_id();
	
	$tok = strtok( $toName,",");
	while ($tok !== false){
		$sql = "INSERT INTO `zy_email` (`emailid`, `addressee`, `read`, `readtime`, `delete`, `bodyid`) VALUES (NULL, '".$tok."', '0', '0', '0', '".$insert_id."');";
		mysql_query($sql,$db);
		$tok = strtok(",");
	}

	$remindurl = "inbox/mailview.php?bodyid=".$insert_id;
	$sql="insert into zy_sms_body(bodyid,sender,smstype,content,sendtime,remindurl) values ('','".$userName."','2','".$title."','".$sendertime."','".$remindurl."')";
	mysql_query($sql,$db);
  
	$bodyid=mysql_insert_id();
	$myarray=explode(",",$toName);  
	$arraycount=count($myarray);
	if($myarray[$arraycount-1]=="")
	$arraycount--;//如果检测到值为1，则再减1来取消该数组
	for($i=0;$i<$arraycount;$i++){
    	if($myarray[$i]=="")
		continue;
		//  $USER_ID_SENT.=$myarray[$i].",";
		$sql="insert into `zy_sms`(`id`,`to_id`,`read`,`delete`,`bodyid`) values (NULL,'".$myarray[$i]."','0','0','".$bodyid."')";	
		mysql_query($sql,$db);
	}
//}
$arrset = array("str"=>"ok");
$result = json_encode($arrset);
echo  $_GET['jsoncallback'].'('.$result.')'; 
?>